const { validationResult } = require('express-validator')
const errorResult = require('../util/error-code')
// 一个错误就终止
module.exports = (validations) => {
  return async (req, res, next) => {
    for (let validation of validations) {
      const result = await validation.run(req)
      if (result.errors.length) break
    }

    const errors = validationResult(req)
    if (errors.isEmpty()) {
      return next()
    }
    // res.status(400).json({ errors: errors.array() })
    res.status(200).json(errorResult[errors.array()[0].msg] || errors.array())
  }
}

// 全部进行验证
// module.exports = (validations) => {
//   return async (req, res, next) => {
//     await Promise.all(validations.map((validation) => validation.run(req)))

//     // errors是验证结果
//     const errors = validationResult(req)
//     if (errors.isEmpty()) {
//       return next()
//     }

//     res.status(400).json({ errors: errors.array() })
//   }
// }
